<template>
	<view class="splash">
		<image src="/static/ic_splash.png" mode="widthFix"></image>
		<text class="title">客满门店管理系统</text>
		<text class="sub-title">店铺管理更方便</text>
	</view>
</template>
<script module="vconsole" lang="renderjs">
	// import VConsole from 'vconsole'
	// new VConsole() // 使用vconsole
</script>
<script setup>
	import {
		reLaunch,
		getUrlParam
	} from '@/utils/util.js'
	import {
		onLoad
	} from '@dcloudio/uni-app'

	import {
		login,
		oauth,
		getUserInfo
	} from '@/api/index.js'
	const wechatAuthLogin = async () => {
		const location = window.location.href
		let redirect_uri = encodeURIComponent(location)
		//wx7b6eba79552ed3b3
		//wx79e738d2b350ddae
		window.location.replace(
			`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7b6eba79552ed3b3&redirect_uri=${redirect_uri}pages/splash/index&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`
		);

	}
	const loginWithCode = async (code) => {
		let res = await login({
			'code': code,
			'state': 'wx7b6eba79552ed3b3'
		})
		console.log(res);
		if (res?.code) {
			console.log(res.data);
			console.log(res.data.token.token);
			uni.setStorageSync('code', code)
			uni.setStorageSync('token', res.data.token.token)
			uni.setStorageSync('userInfo', res.data)
			const store_sn = getUrlParam('store_sn');
			if (store_sn) {
				uni.setStorageSync('store_sn', store_sn)
				reLaunch({
					url: `/pages/main/index`
				})
			} else {
				reLaunch({
					url: `/pages/shopList/index`
				})
			}
		}
	}

	onLoad((options) => {
		const code = getUrlParam('code');
		if (code) {
			console.log('code', code)
			// get user info
			loginWithCode(code)
			//如果已经授权过，直接再次登录，避免token过期
		} else {
			wechatAuthLogin()
			// reLaunch({
			// 	url: `/pages/shopList/index`
			// })
		}
		// uni.login({
		// 	provider: 'weixin',
		// 	success: (res) => {
		// 		console.log('login', res);
		// 		loginWithCode(res.code)
		// 	},
		// 	fail: (err) => {
		// 		console.log('登录失败', err);
		// 	}
		// })

	});
</script>

<style lang="scss" scoped>
	.splash {
		width: 100vw;
		height: 100vh;
		background: white;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		image {
			width: 651rpx;
		}

		.title {
			margin-top: 58rpx;
			font-size: 42rpx;
			color: #151515;
			font-weight: bold;
		}

		.sub-title {
			margin-top: 16rpx;
			font-size: 34rpx;
			margin-bottom: 74rpx;
			color: #737B79;
		}
	}
</style>